#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->m_editBool->setMinimumWidth(100);
    ui->m_editInt->setMinimumWidth(100);
    ui->m_editFlot->setMinimumWidth(100);

    m_Thread=new Thread;
    connect(m_Thread,&Thread::sig_UpdataUI,this,&MainWindow::slots_UpdataUI);
    ui->m_editFlot->setObjectName("0");
    ui->m_editInt->setObjectName("1");
    ui->m_editBool->setObjectName("2");
    connect(ui->m_editFlot,&QLineEdit::editingFinished,this,&MainWindow::slots_editIsOK);
    connect(ui->m_editInt,&QLineEdit::editingFinished,this,&MainWindow::slots_editIsOK);
    connect(ui->m_editBool,&QLineEdit::editingFinished,this,&MainWindow::slots_editIsOK);
}

MainWindow::~MainWindow()
{
    delete ui;
    //m_Thread->deleteLater();
    delete m_Thread;
    m_Thread=nullptr;
}

void MainWindow::slots_UpdataUI()
{
    Recdata UpData=m_Thread->getRecData();
    //qDebug()<<"slots_UpdataUI";
    if(!ui->m_editFlot->isModified()){ ui->m_editFlot->setText(QString::number(UpData.data_float)); }
    if(!ui->m_editInt->isModified()){ ui->m_editInt->setText(QString::number(UpData.data_int)); }
    if(!ui->m_editBool->isModified()){ ui->m_editBool->setText(QString::number(UpData.data_bool)); }
}

void MainWindow::slots_editIsOK()
{
    QLineEdit *TmpLineEdit=qobject_cast<QLineEdit *>(sender());
    unsigned Number=TmpLineEdit->objectName().toUInt();

    QVariant data=0;
    if(!TmpLineEdit->text().isEmpty())
    {
        data=TmpLineEdit->text();
    }

    switch (Number) {
    case 0:
        ui->m_editFlot->setText(data.toString());
        break;
    case 1:
        ui->m_editInt->setText(data.toString());
        break;
    case 2:
        ui->m_editBool->setText(data.toString());
        break;
    default:
        break;
    }

    emit m_Thread->sig_wirteModbus(Number,data);
}



